package logic

import (
	"context"
	"fmt"
	"gorm.io/gorm"
	"store/models"

	"store/storerpc/internal/svc"
	"store/storerpc/storerpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type StoreUpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewStoreUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *StoreUpdateLogic {
	return &StoreUpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 门店修改
func (l *StoreUpdateLogic) StoreUpdate(in *storerpc.StoreUpdateRequest) (*storerpc.StoreUpdateResponse, error) {
	// todo: add your logic here and delete this line

	var store models.Store
	models.DB.Debug().Where("id = ?", in.StoreId).Find(&store)
	if store.ID == 0 {
		return nil, fmt.Errorf("门店不存在,修改失败")
	}

	store = models.Store{
		Model:   gorm.Model{ID: uint(in.StoreId)},
		Name:    in.Name,
		Address: in.Address,
		UserId:  in.UserId,
	}
	models.DB.Debug().Updates(&store)

	return &storerpc.StoreUpdateResponse{
		StoreId: int64(store.ID),
	}, nil
}
